/**
 * @Author: douxuefeng
 * @Date: 2020/11/19 上午9:20
 * @File: init_app
 * @Desc:
 */
package app
import (
	comm "gitee.com/douxuefeng/go-common"
	"gitee.com/douxuefeng/go-common/cache"
	"gitee.com/douxuefeng/go-iris/config"
	"gitee.com/douxuefeng/go-iris/model"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	log "github.com/sirupsen/logrus"
	"os"
	"time"
)

func InitApp()  {
	var err error
	conf := config.GetConfig()
	mysql := conf.MySql
	// 创建文件夹
	if !comm.IsExist("./statics/") {
		comm.CreateFile("./statics/")
	}
	if !comm.IsExist("./view/") {
		comm.CreateFile("./view")
	}
	// 初始化日志系统
	if !comm.IsExist(conf.Logs) {
		comm.CreateFile(conf.Logs)
	}
	log.SetFormatter(&log.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	})
	log.SetOutput(os.Stdout)
	log.SetLevel(log.DebugLevel)
	f, _ := rotatelogs.New(
		conf.Logs + time.Now().Format("2006-01-02") + ".log",
		rotatelogs.WithLinkName(conf.Logs + "logs.log"),
		rotatelogs.WithMaxAge(time.Duration(30 * 24) * time.Hour),
		rotatelogs.WithRotationTime(time.Duration(24)*time.Hour),
	)
	log.SetOutput(f)
	// 初始化数据库
	err = comm.InitDataBase(mysql.UserName, mysql.Password, mysql.Host, mysql.DataBase, mysql.Prefix, mysql.LogMode, mysql.MaxIdle, mysql.MaxOpen, model.Models...)
	if err != nil {
		log.Fatal(err)
	}
	// 初始化redis
	cache.InitRedis(&cache.RedisOpt{
		Host:        conf.Redis.Host,
		Password:    conf.Redis.Password,
		Database:    conf.Redis.DataBase,
		MaxIdle:     conf.Redis.MaxIdle,
		MaxActive:   conf.Redis.MaxActive,
		IdleTimeout: conf.Redis.IdleTimeout,
	})
	// 初始化snowflake 单机订单号
	//comm.InitSnowflake(1599190883, 1)
}

